package vista;

import java.awt.Graphics;

import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import java.util.ArrayList;

import java.util.List;


import javax.swing.JPanel;


import constants.Constants;
import controller.Controller;
/**
 * this is the construct class that will let us to create the enviroment 
 * of the board
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class BoardArea extends JPanel implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BufferedImage boardImage;
	private List<Image> playerImages;
	private int imageIndex = 0;
	private int posX;
	private int posY;
	private Polygon[] squares;
	List<GraphicPlayer> players;
	private Controller gameController;
	private JPanel playerDice;
	
	/**
	 * this method construct a new Board area with the currents param for the game play.
	 * @param bounds the size of our board
	 * @param boardImage the main image of the board
 	* @param playerImages the players image to put on the board
 	* @param gameController the main controler of the board
	 * @param dicesImages the images that will be added to the context in gameplay 
 	*/
	public BoardArea(Rectangle bounds, final BufferedImage boardImage,List<Image> playerImages,Controller gameController) {

		this.gameController = gameController;
		
		this.players = new ArrayList<GraphicPlayer>();
		this.playerImages = playerImages;
		
		this.setBounds(bounds);
		this.boardImage = boardImage;
		this.setLayout(null);
		
		
		
		this.addMouseListener(this);
		//setPlayerInConfesionario(2,boardImage);
		setPolygons();
	}
	
	
	/**
	 * this method will add a graphic player to the Board with a different player image for each player.
	 * @param playerId the relative number of the player to be added
	 */
	public void addPlayer(int playerId) {
		if(playerImages.get(imageIndex)!= null)
			players.add(new GraphicPlayer(new Rectangle(701,176,40,80),playerImages.get(playerId-1)));  //imageIndex++
	}

	/**
	 * this method move a player in our validate graph of the board.
	 * @param square the graph square were we want to get
	 * @param player the player that we want to move
	 */
	public void movePlayer(int square,int player) {
		GraphicPlayer p = players.get(player -1);
		Polygon s = squares[square -1];
		Rectangle bounds = s.getBounds();
		p.setPlayerMove(new Point(bounds.x+bounds.width/2,bounds.y+bounds.height/2));
	}
	
	
	/**
	 * this method will paint our image in this panel
	 */
	public void paintComponent(Graphics g) {
		
		g.drawImage(boardImage, getBounds().x, getBounds().y,getBounds().width, getBounds().height, null);
		
		for(GraphicPlayer player : players) 
			player.render(g);
				
	}
	/**
	 * this method will set the zones poligons area for each rooms of the board
	 */
	private void setPolygons() {
		squares = new Polygon[25];

		Polygon capilla = new Polygon(); // 0
		capilla.addPoint(353, 127);
		capilla.addPoint(455, 60);
		capilla.addPoint(485, 50);
		capilla.addPoint(528, 60);
		capilla.addPoint(533, 93);
		capilla.addPoint(527, 105);
		capilla.addPoint(420, 189);
		capilla.addPoint(359, 130);

		capilla.addPoint(487, 45);
		squares[0] = capilla;

		Polygon confesionario1 = new Polygon(); // 1
		confesionario1.addPoint(416, 76);
		confesionario1.addPoint(447, 57);
		confesionario1.addPoint(427, 35);
		confesionario1.addPoint(406, 39);
		confesionario1.addPoint(396, 54);
		confesionario1.addPoint(417, 79);
		squares[1] = confesionario1;

		Polygon cripta = new Polygon(); // 2
		cripta.addPoint(545, 81);
		cripta.addPoint(615, 79);
		cripta.addPoint(622, 94);
		cripta.addPoint(654, 97);
		cripta.addPoint(633, 95);
		cripta.addPoint(654, 121);
		cripta.addPoint(604, 143);
		cripta.addPoint(567, 135);
		squares[2] = cripta;

		Polygon patio = new Polygon(); // 2
		patio.addPoint(325, 263);
		patio.addPoint(248, 169);
		patio.addPoint(261, 135);
		patio.addPoint(352, 137);
		patio.addPoint(414, 195);
		patio.addPoint(484, 148);
		patio.addPoint(517, 182);
		patio.addPoint(568, 203);
		patio.addPoint(506, 208);
		patio.addPoint(511, 254);
		patio.addPoint(442, 264);
		patio.addPoint(380, 245);
		squares[3] = patio;

		Polygon Confesionario2 = new Polygon(); // 2
		Confesionario2.addPoint(492, 145);
		Confesionario2.addPoint(517, 123);
		Confesionario2.addPoint(541, 144);
		Confesionario2.addPoint(534, 159);
		Confesionario2.addPoint(516, 159);
		Confesionario2.addPoint(494, 147);
		squares[4] = Confesionario2;

		Polygon Claustro1 = new Polygon(); // 2
		Claustro1.addPoint(318, 277);
		Claustro1.addPoint(390, 259);
		Claustro1.addPoint(436, 268);
		Claustro1.addPoint(456, 284);
		Claustro1.addPoint(393, 327);
		Claustro1.addPoint(373, 321);
		squares[5] = Claustro1;

		Polygon Claustro2 = new Polygon(); // 2
		Claustro2.addPoint(307, 281);
		Claustro2.addPoint(285, 316);
		Claustro2.addPoint(285, 365);
		Claustro2.addPoint(299, 394);
		Claustro2.addPoint(364, 345);
		Claustro2.addPoint(362, 331);
		squares[6] = Claustro2;
		// Claustro3
		Polygon Claustro3 = new Polygon(); // 2
		Claustro3.addPoint(396, 350);
		Claustro3.addPoint(452, 403);
		Claustro3.addPoint(482, 358);
		Claustro3.addPoint(466, 294);
		Claustro3.addPoint(480, 341);
		Claustro3.addPoint(466, 296);
		Claustro3.addPoint(397, 338);
		squares[7] = Claustro3;

		Polygon Hall1 = new Polygon(); // 2
		Hall1.addPoint(452, 272);
		Hall1.addPoint(600, 266);
		Hall1.addPoint(608, 290);
		Hall1.addPoint(480, 301);
		Hall1.addPoint(466, 285);
		squares[8] = Hall1;

		Polygon Celda1 = new Polygon(); // 2
		Celda1.addPoint(454, 218);
		Celda1.addPoint(593, 213);
		Celda1.addPoint(601, 248);
		Celda1.addPoint(524, 256);
		squares[9] = Celda1;

		Polygon Scriptorium = new Polygon(); // 2
		Scriptorium.addPoint(487, 310);
		Scriptorium.addPoint(660, 294);
		Scriptorium.addPoint(667, 320);
		Scriptorium.addPoint(648, 320);
		Scriptorium.addPoint(622, 366);
		Scriptorium.addPoint(485, 372);
		Scriptorium.addPoint(490, 341);
		squares[10] = Scriptorium;

		Polygon Biblioteca = new Polygon(); // 2
		Biblioteca.addPoint(631, 363);
		Biblioteca.addPoint(654, 328);
		Biblioteca.addPoint(703, 326);
		Biblioteca.addPoint(722, 355);
		Biblioteca.addPoint(730, 359);
		Biblioteca.addPoint(712, 388);
		Biblioteca.addPoint(661, 396);
		squares[11] = Biblioteca;

		Polygon Hall2 = new Polygon(); // 2
		Hall2.addPoint(458, 408);
		Hall2.addPoint(483, 383);
		Hall2.addPoint(505, 379);
		Hall2.addPoint(544, 400);
		Hall2.addPoint(501, 448);
		squares[12] = Hall2;

		Polygon Celda2 = new Polygon(); // 2
		Celda2.addPoint(556, 407);
		Celda2.addPoint(512, 452);
		Celda2.addPoint(544, 489);
		Celda2.addPoint(602, 427);
		squares[13] = Celda2;

		Polygon Claustro4 = new Polygon(); // 2
		Claustro4.addPoint(369, 354);
		Claustro4.addPoint(389, 355);
		Claustro4.addPoint(441, 408);
		Claustro4.addPoint(374, 420);
		Claustro4.addPoint(306, 397);
		Claustro4.addPoint(367, 356);
		squares[14] = Claustro4;

		Polygon Hall3 = new Polygon(); // 2
		Hall3.addPoint(416, 428);
		Hall3.addPoint(454, 415);
		Hall3.addPoint(493, 454);
		Hall3.addPoint(419, 481);
		Hall3.addPoint(414, 431);		
		squares[15] = Hall3;		

		Polygon Celda3 = new Polygon(); // 2
		Celda3.addPoint(452, 487);
		Celda3.addPoint(501, 458);
		Celda3.addPoint(535, 494);
		Celda3.addPoint(487, 527);
		squares[16] = Celda3;

		Polygon SalaCapitular = new Polygon(); // 2
		SalaCapitular.addPoint(347, 428);
		SalaCapitular.addPoint(405, 431);
		SalaCapitular.addPoint(408, 494);
		SalaCapitular.addPoint(447, 551);
		SalaCapitular.addPoint(426, 565);
		SalaCapitular.addPoint(322, 568);
		SalaCapitular.addPoint(304, 541);
		SalaCapitular.addPoint(326, 494);
		SalaCapitular.addPoint(346, 492);
		squares[17] = SalaCapitular;

		Polygon Hall4 = new Polygon(); // 2
		Hall4.addPoint(297, 408);
		Hall4.addPoint(332, 432);
		Hall4.addPoint(330, 478);
		Hall4.addPoint(280, 462);
		Hall4.addPoint(251, 439);
		Hall4.addPoint(299, 408);
		squares[18] = Hall4;

		Polygon Celda4 = new Polygon(); // 2
		Celda4.addPoint(265, 473);
		Celda4.addPoint(311, 488);
		Celda4.addPoint(293, 527);
		Celda4.addPoint(232, 501);
		squares[19] = Celda4;
		
		Polygon Hall5 = new Polygon(); // 2
		Hall5.addPoint(123, 361);
		Hall5.addPoint(267, 359);
		Hall5.addPoint(289, 394);
		Hall5.addPoint(237, 429);
		Hall5.addPoint(198, 376);
		Hall5.addPoint(129, 403);
		squares[20] = Hall5;
		
		Polygon Celda5 = new Polygon(); // 2
		Celda5.addPoint(193, 400);
		Celda5.addPoint(223, 437);
		Celda5.addPoint(176, 475);
		Celda5.addPoint(140, 421);
		squares[21] = Celda5;		

		
		Polygon Locutorio = new Polygon(); // 2
		Locutorio.addPoint(125, 340);
		Locutorio.addPoint(265, 346);
		Locutorio.addPoint(274, 309);
		Locutorio.addPoint(204, 303);
		Locutorio.addPoint(207, 279);
		Locutorio.addPoint(127, 280);
		squares[22] = Locutorio;
		
		Polygon Hall6 = new Polygon(); // 2
		Hall6.addPoint(295, 291);
		Hall6.addPoint(308, 265);
		Hall6.addPoint(286, 233);
		Hall6.addPoint(229, 274);
		Hall6.addPoint(230, 290);
		squares[23] = Hall6;
		
		Polygon Celda6 = new Polygon(); // 2
		Celda6.addPoint(274, 212);
		Celda6.addPoint(240, 183);
		Celda6.addPoint(176, 220);
		Celda6.addPoint(221, 251);
		squares[24] = Celda6;

	}

	/**
	 * this method will let us know when player has make a click on the mouse to move
	 * his avatar to the next location
	 */
	@Override
	public void mouseClicked(MouseEvent arg0) {
		posX = arg0.getX();
		posY = arg0.getY();
		
		if((GameLogic.gameState & Constants.MOVEMENT) > 0)
			for(int i=0;i<squares.length;i++) 
				if(squares[i].contains(posX, posY)) 
					gameController.movePlayer(i+1);
	}

	/**
	 * deprecate NOT IMPLEMENTED!
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	/**
	 * deprecate NOT IMPLEMENTED!
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	/**
	 * deprecate NOT IMPLEMENTED!
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}
	/**
	 * deprecate NOT IMPLEMENTED!
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}

}
